﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IFSModel;


namespace JobAt6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            const int addStar = 10;
            using (var dataContext = new IFNDBDataContext())
            {
                try
                {
                    var userTrans = new List<Members_point_tran>();
                    var memberNoti = new List<IFSModel.MemberNotification>();

                    var users = from user in dataContext.Members
                                where user.Curent_Star < 10
                                select user;
                    foreach (var m in users)
                    {
                        var member =
                                       dataContext.Members.Single(prop => prop.MemID == m.MemID);
                        if (member != null)
                        {
                            var oldstar = member.Curent_Star;
                            member.Curent_Star = addStar;
                            member.Updatedate = DateTime.Now;
                            userTrans.Add(new Members_point_tran
                            {
                                MemID = member.MemID,
                                TypeID = 2,
                                Createdate = DateTime.Now,
                                Mpt_Cal_Star = addStar - member.Curent_Star,
                                Mpt_Cal_Point = 0,
                                Active = 1
                            });
                            if (oldstar != null)
                            {
                                int addStarTran = addStar - Convert.ToInt32(oldstar);
                                memberNoti.Add(new IFSModel.MemberNotification { message = "", UserName = member.UserName, star = addStarTran, notiTypeId = 2, isRead = false, isDisable = false, point = 0 });
                            }
                        }
                        

                        
                    }
                    dataContext.MemberNotifications.InsertAllOnSubmit(memberNoti);
                    dataContext.Members_point_trans.InsertAllOnSubmit(userTrans);
                    dataContext.SubmitChanges();
                    
                }
                catch (Exception ex)
                {
                   
                   
                }
            }

        }
    }
}
